<?php 
	$selected = $_GET['selected'];
	$ifont = 0;
	if(isset($_POST['imagefont']))
	{$ifont = '1';}
	
	if($selected == "")
	{
		if($_POST['name'] != "")
		{
			addMenuTemplate($_POST['name'], $_POST['menu_type']);
		}
		else
		{
			$error = "Must fill in name";
		}
	}
	
	if($selected != "")
	{
		$item = getMenuTemplate($selected);
		if($item['parent_id'] > 0)
		{
			$parent_item = getMenuTemplate($item['parent_id']);
		}
		if(($_GET['active'] != $item['active']) && ($_GET['active'] != ""))
		{
			$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET active = '".$_GET['active']."' WHERE id = '".$item['id']."'");
		}
		if(($_GET['type'] != "") && ($item['id'] > 0))
		{  setMenuTempType($item['id'], $_GET['type']); }
		
		if($_POST['add_level'] > 0)
		{ addMenuLevel($_POST['add_level']); }
		
		if($_POST['action'] == "update_header")
		{
			$style = $_POST['style'];
			$style = $style.$_POST['align'];
			$style = $style.$_POST['valign'];
			
			if($_POST['name'] != $item['name'])
			{ setMenuTemplateName($item['id'], $_POST['name']); }
			
			if($_POST['height'] != $item['height'])
			{
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET height= '".$_POST['height']."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$heightStatus = "Height set to <strong>".$_POST['height']."</strong>";}
			}
			if($style != $item['style'])
			{
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET style = '".$style."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$StyleStatus = "Style changed to <strong>".$_POST['style']."</strong>";}
			}
			if($ifont != $item['imagefont'])
			{
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET imagefont = '".$ifont."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$ifontStatus = "Image Font set to <strong>".$ifont."</strong>";}
			}	
			if(($_POST['delete_l'] == "1") && ($item['image_l'] != ""))
			{
				if (!unlink(MENU_PATH.$item['image_l']))
				{ $lImageStatus = "Error deleting ".MENU_PATH.$item['image_l']; }
				else
				{ 
					$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_l = '' WHERE id = '".$item['id']."'");
					if ($result > 0)
					{$lImageStatus = "Left Image Deleted";}
				}
			}
			if(($_POST['delete_r'] == "1") && ($item['image_r'] != ""))
			{
				if (!unlink(MENU_PATH.$item['image_r']))
				{ $rImageStatus = "Error deleting ".MENU_PATH.$item['image_r']; }
				else
				{ 
					$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_r = '' WHERE id = '".$item['id']."'");
					if ($result > 0)
					{$rImageStatus = "Right Image Deleted";}
				}
			}
			
			if(($_POST['delete_img'] == "1") && ($item['image'] != ""))
			{
				if (!unlink(MENU_PATH.$item['image']))
				{ $ImageStatus = "Error deleting ".MENU_PATH.$item['image']; }
				else
				{ 
					$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image = '' WHERE id = '".$item['id']."'");
					if ($result > 0)
					{$ImageStatus = "Image Deleted";}
				}
			}
			if(($_POST['delete_bg'] == "1") && ($item['image_bg'] != ""))
			{
				if (!unlink(MENU_PATH.$item['image_bg']))
				{ $bgImageStatus = "Error deleting ".MENU_PATH.$item['image_bg']; }
				else
				{ 
					$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_bg = '' WHERE id = '".$item['id']."'");
					if ($result > 0)
					{$bgImageStatus = "Bg Image Deleted";}
				}
			}
			if(($_POST['delete_hov'] == "1") && ($item['image_hov'] != ""))
			{
				if (!unlink(MENU_PATH.$item['image_hov']))
				{ $hovImageStatus = "Error deleting ".MENU_PATH.$item['image_hov']; }
				else
				{ 
					$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_hov = '' WHERE id = '".$item['id']."'");
					if ($result > 0)
					{$hovImageStatus = "Image Deleted";}
				}
			}
			if(($_POST['delete_act'] == "1") && ($item['image_act'] != ""))
			{
				if (!unlink(MENU_PATH.$item['image_act']))
				{ $hovImageStatus = "Error deleting ".MENU_PATH.$item['image_act']; }
				else
				{ 
					$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_act = '' WHERE id = '".$item['id']."'");
					if ($result > 0)
					{$actImageStatus = "Image Deleted";}
				}
			}
		}
	}
	if (($item['id'] > 0) && ($_POST['action'] != ""))
	{
		if (image_valid($_FILES['image']['type']) && ($_FILES["file"]["size"] < 20000) && ($_FILES['image']['name'] != ""))
		{
			$temp_name = $_FILES["image"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$item['template_id']."_".$item['level']."_img.".$extension;
			
			if (file_exists(MENU_PATH.$new_name))
			{$error = $_FILES["image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image"]["tmp_name"], MENU_PATH.$new_name);
				$error = "Stored in: " . MENU_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image = '".$new_name."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$ImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		
		if (image_valid($_FILES['image_bg']['type']) && ($_FILES["file"]["size"] < 20000) && ($_FILES['image_bg']['name'] != ""))
		{
			$temp_name = $_FILES["image_bg"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$item['template_id']."_".$item['level']."_bg.".$extension;
			
			if (file_exists(MENU_PATH.$new_name))
			{$error = $_FILES["image_bg"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image_bg"]["tmp_name"], MENU_PATH.$new_name);
				$error = "Stored in: " . MENU_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_bg = '".$new_name."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$bgImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['image_hov']['type']) && ($_FILES["file"]["size"] < 20000) && ($_FILES['image_hov']['name'] != ""))
		{
			$temp_name = $_FILES["image_hov"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$item['template_id']."_".$item['level']."_hov.".$extension;
			
			if (file_exists(MENU_PATH.$new_name))
			{$error = $_FILES["image_hov"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image_hov"]["tmp_name"], MENU_PATH.$new_name);
				$error = "Stored in: " . MENU_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_hov = '".$new_name."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$hovImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['image_act']['type']) && ($_FILES["file"]["size"] < 20000) && ($_FILES['image_act']['name'] != ""))
		{
			$temp_name = $_FILES["image_act"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$item['template_id']."_".$item['level']."_act.".$extension;
			
			if (file_exists(MENU_PATH.$new_name))
			{$error = $_FILES["image_act"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image_act"]["tmp_name"], MENU_PATH.$new_name);
				$error = "Stored in: " . MENU_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_act = '".$new_name."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$actImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['image_l']['type']) && ($_FILES["image_l"]["size"] < 20000) && ($_FILES['image_l']['name'] != ""))
		{
			$l_temp_name = $_FILES["image_l"]["name"];
			$l_extension = substr($l_temp_name,-3);
			$l_new_name = "type_".$item['template_id']."_".$item['level']."_l.".$l_extension;
			
			if (file_exists(MENU_PATH.$l_new_name))
			{$error = $_FILES["image_l"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image_l"]["tmp_name"], MENU_PATH.$l_new_name);
				$error = "Stored in: " . MENU_PATH.$l_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_l = '".$l_new_name."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$lImageStatus = "<strong>Image Uploaded ".$l_new_name."</strong>";}
			}
		}
		if (image_valid($_FILES['image_r']['type']) && ($_FILES["image_r"]["size"] < 20000) && ($_FILES['image_r']['name'] != ""))
		{
			$r_temp_name = $_FILES["image_r"]["name"];
			$r_extension = substr($r_temp_name,-3);
			$r_new_name = "type_".$item['template_id']."_".$item['level']."_r.".$r_extension;
			
			if (file_exists(MENU_PATH.$r_new_name))
			{$error = $_FILES["image_r"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image_r"]["tmp_name"], MENU_PATH.$r_new_name);
				$error = "Stored in: " . MENU_PATH.$r_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."menu_templates SET image_r = '".$r_new_name."' WHERE id = '".$item['id']."'");
				if ($result > 0)
				{$rImageStatus = "<strong>Image Uploaded ".$r_new_name."</strong>";}
			}
		}
	}
if($selected != "")
{
	$item = getMenuTemplate($selected);
}
?>